//
//  WBHomeViewController.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/4/7.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

fileprivate let originalCellID = "originalCellID"
fileprivate let reteewtedCellID = "reteewtedCellID"

class WBHomeViewController: WBBaseViewController {
    
    //懒加载数据源的数组
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    override func loadData() {
        
        listViewModel.loadStatus(pullUp: isPullUp) { (isSuccess,shouldRefresh) in
            
            print("刷新数据")
            
            ///结束下拉刷新
            self.refreshControl?.endRefreshing()
            
            ///设置上拉的可用
            self.isPullUp = false
            
            if shouldRefresh{
                
                self.tableView?.reloadData()
                
            }
        }
    }

    @objc fileprivate func showFirends(){
        
        let vc = WBDemoViewController()
        
        vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: - 实现数据原方法
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //0: 先去viewModel
        let viewModel = listViewModel.statusList[indexPath.row]
        
        let cellID = viewModel.status.retweeted_status == nil ? originalCellID : reteewtedCellID
        
        //1:取缓冲池中的cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell
        

        //2:给cell赋值
        cell.viewModel = viewModel
        
        //2.5:设置cell的代理
        cell.delegate = self
        
        //3:返回cell
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        //1:获取视图模型
        let vm = listViewModel.statusList[indexPath.row]
        
        //2:返回行高
        return vm.rowHeight
    }
    
}

extension WBHomeViewController: WBStatusCellDelegate{
    
    func didSelectedStatusCell(statusCell: WBStatusCell, url: String) {
        
        let vc = WBWebViewController()
        
        vc.urlString = url
        
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: - 设置UI
extension WBHomeViewController{
    
    override func setupTableView() {
        super.setupTableView()
        
        
        //1设置导航栏 但是这种方法无法高亮
        //        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFirends))
        
        //2:这种设置可以高亮了,但是由于项目中很多地方用到,所以一定要做代码的抽取
        //Swift调用OC的返回instancetype的方法的时候,判断不出来返回值是不是可选的,那都被任务是可选的,
        //在定义的后面加一个类型就可以了
        //        let btn: UIButton = UIButton.cz_textButton("添加好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        //
        //        btn.addTarget(self, action: #selector(showFirends), for: .touchUpInside)
        //
        //        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        //3:利用分类和便利构造函数,抽取UIBarButtonItem的自定义
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFirends))
        
        //注册一个原创微博的cell
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellID)
        //注册一个转发微博的cell
        tableView?.register(UINib(nibName: "WBStatusReteewtedCell", bundle: nil), forCellReuseIdentifier: reteewtedCellID)
        
        //设置tableView的属性
//        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        tableView?.separatorStyle = .none
        
        setnavTitle()
    }
    
    fileprivate func setnavTitle() {
        
        let title = WBNetworkManager.shared.userAccount.screen_name
        
        let btn = WBTitleButton(title: title)
        
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        
        navItem.titleView = btn
    }
    
    @objc fileprivate func btnClick(btn: UIButton){
        
        btn.isSelected = !btn.isSelected
    }
}
